package com.glp.work.flow.client;

import com.glp.common.enums.ReturnEnum;
import com.glp.common.reponse.BaseResult;
import com.glp.work.flow.request.OrderApprovalDTO;
import com.glp.work.flow.request.OrderBatchApprovalDTO;
import com.glp.work.flow.response.OrderBatchApprovalResponse;
import com.glp.work.flow.response.TaskExecutionInfoResponse;
import feign.hystrix.FallbackFactory;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 外部系统发起工作流
 *
 * @author pengjia
 * @version 2020年3月9日 下午1:04:33
 */

@FeignClient(value = "${work.flow.feign.server.key:GLP-WORK-FLOW}", path = "/api/process", fallbackFactory = WfProcessClient.ClientFallback.class)
public interface WfProcessClient {

    /**
     * 工单审批接口  需要先领取任务，才能进行审批任务
     */
    @ApiOperation(value = "工单审批接口")
    @PostMapping(value = "/client/orderApproval")
    @ResponseBody
    public BaseResult<TaskExecutionInfoResponse> orderApproval(@RequestBody OrderApprovalDTO orderApprovalDTO);

    @ApiOperation(value = "工单审批接口Feign 2.0(没有领取任务的动作)")
    @PostMapping(value = "/client/orderApprovalTwo")
    @ResponseBody
    public BaseResult<TaskExecutionInfoResponse> orderApprovalTwo(@RequestBody OrderApprovalDTO orderApprovalDTO);

    @ApiOperation(value = "工单批量审批接口")
    @PostMapping(value = "/client/orderBatchApproval")
    @ResponseBody
    public BaseResult<OrderBatchApprovalResponse> orderBatchApproval(@RequestHeader String userNo, @RequestBody OrderBatchApprovalDTO orderBatchApprovalDTO);

    @Component
    @Slf4j
    public class ClientFallback implements FallbackFactory<WfProcessClient> {
        @Override
        public WfProcessClient create(Throwable cause) {
            return new WfProcessClient() {
                @Override
                public BaseResult<TaskExecutionInfoResponse> orderApproval(OrderApprovalDTO orderApprovalDTO) {
                    log.error("工作流审批失败！{}" + orderApprovalDTO, cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "工作流审批失败！{}" + orderApprovalDTO);
                }

                @Override
                public BaseResult<TaskExecutionInfoResponse> orderApprovalTwo(OrderApprovalDTO orderApprovalDTO) {
                    log.error("工作流审批失败！{}" + orderApprovalDTO, cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "工作流审批失败！{}" + orderApprovalDTO);
                }

                @Override
                public BaseResult<OrderBatchApprovalResponse> orderBatchApproval(String userNo, OrderBatchApprovalDTO orderBatchApprovalDTO) {
                    log.error("批量工单审批失败！{}" + orderBatchApprovalDTO, cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "批量工单审批失败！{}" + orderBatchApprovalDTO);
                }

            };
        }
    }

}
